package com.lanou.tipsy.adapter.recommend;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.tipsy.R;
import com.lanou.tipsy.entity.recommend.guess.songlist.GuessListEntity;

import java.util.ArrayList;

/**
 * Create by FengJun
 * 推荐-猜你喜欢-今日歌单适配器
 */

public class TodaySongsListsAdapter extends
        RecyclerView.Adapter<TodaySongsListsAdapter.MyViewHolder> {
    private static final String TAG = "TodaySongsListsAdapter";
    private ArrayList<GuessListEntity> mDatas;
    private LayoutInflater mInflater;
    private GuessListEntity data;

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public TodaySongsListsAdapter(Context context, ArrayList<GuessListEntity> datas) {
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }


    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        if (mDatas != null && mDatas.size() > 0) {
            data = mDatas.get(position);
            ViewGroup.LayoutParams lp =
                    holder.linearLayout.getLayoutParams();
            holder.linearLayout.setLayoutParams(lp);
            if (position < 9) {
                holder.number.setText("0" + (position + 1));
            } else {
                holder.number.setText("" + (position + 1));
            }
            holder.singer_name.setText(data.getAuthor());
            holder.song_name.setText(data.getTitle());
        }

        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null) {
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.linearLayout, pos);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    class MyViewHolder extends ViewHolder {

        private LinearLayout linearLayout;
        private TextView number;
        private TextView singer_name;
        private TextView song_name;

        public MyViewHolder(View view) {
            super(view);
            linearLayout = (LinearLayout) view.findViewById(R.id.li_recommend);
            singer_name = (TextView) view.findViewById(R.id.tv_re_singer_name);
            song_name = (TextView) view.findViewById(R.id.tv_re_song_name);
            number = (TextView) view.findViewById(R.id.tv_re_number);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(mInflater.inflate(
                R.layout.item_recommend_everyday, parent, false));
        return holder;
    }
}